﻿// PortableMediator
// (c) Lance Wynn - http://PortableMediator.codeplex.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PortableMediator
{
    public static class MediatorExtensions
    {
        public static IEnumerable<MethodInfo> GetExposedMethods(this Type type)
        {
            if (type != null)
            {
                foreach (var method in type.GetTypeInfo().DeclaredMethods)
                {
                    yield return method;
                }
                foreach (var method in type.GetTypeInfo().BaseType.GetExposedMethods())
                {
                    yield return method;
                }
            }
        }

        public static MethodInfo GetExposedMethods(this Type type, string name)
        {
            return type.GetExposedMethods().Where(m => m.Name == name).FirstOrDefault();
        }

        public static IEnumerable<PropertyInfo> GetExposedProperties(this Type type)
        {
            if (type != null)
            {
                foreach (var prop in type.GetTypeInfo().DeclaredProperties)
                {
                    yield return prop;
                }
                foreach (var prop in type.GetTypeInfo().BaseType.GetExposedProperties())
                {
                    yield return prop;
                }
            }
        }

        public static PropertyInfo GetExposedProperties(this Type type, string name)
        {
            return type.GetExposedProperties().Where(p => p.Name == name).FirstOrDefault();
        }
    }
}
